import { Context, PlainObject } from "egg";
import { Op } from "sequelize";
import helper from "../helper";
import EntityDao from "../dao";
import { DaoService } from "../mvc";

export default class PlatformService extends DaoService {
  constructor(ctx: Context) {
    super(ctx);
    this.dao = new EntityDao(ctx, ctx.model.Platform);
  }

  /**
   * 登记流媒体平台
   * @param data
   * @returns
   */
  async create(data: any) {
    return await this.dao.create(data, ["pltName", "pltProtocol", "pltIp", "pltPort", "pltSecret"]);
  }

  /**
   * 删除流媒体平台信息
   * @param pltId 平台id
   * @returns
   */
  async destroy(pltId: string): Promise<ServiceResponse> {
    return await this.dao.destroy(pltId);
  }

  /**
   * 修改流媒体平台信息
   * @param pltId
   * @param data
   * @returns
   */
  async update(pltId: string, data: any): Promise<ServiceResponse> {
    return await this.dao.update(pltId, data, ["pltName", "pltProtocol", "pltIp", "pltPort", "pltSecret"]);
  }

  /**
   * 查询平台列表
   * @param pltName 平台名称
   */
  async index(query: PlainObject<string>): Promise<ServiceResponse> {
    const where: any = {};
    if (!helper.isEmpty(query.pltName)) {
      where.pltName = { [Op.like]: `%${query.pltName}%` };
    }
    if (!helper.isEmpty(query.pltState)) {
      where.pltState = query.pltState;
    }

    return await this.dao.index(where);
  }

  /**
   * 查询平台明细
   * @param pltId
   * @returns
   */
  async show(pltId: string): Promise<ServiceResponse> {
    return this.dao.show(pltId);
  }

  async page(query: PlainObject<string>): Promise<ServiceResponse> {
    const where: any = {};
    if (!helper.isEmpty(query.pltName)) {
      where.pltName = { [Op.like]: `%${query.pltName}%` };
    }
    if (!helper.isEmpty(query.pltState)) {
      where.pltState = query.pltState;
    }

    return await this.dao.page({ current: query.current, pageSize: query.pageSize, where });
  }
}
